package core

import (
	"gin-demo/sysinit"
	"mime/multipart"

	"github.com/gin-gonic/gin"
)

// UploadService 文件上传服务
type UploadService struct {
}

// Create 创建文件上传实例
func (s UploadService) Create(file *multipart.FileHeader, ctx *gin.Context) (upload Upload, err error) {
	upload = Upload{Name: file.Filename}
	upload.SetPath()
	if err = ctx.SaveUploadedFile(file, upload.FullPath()); err == nil {
		err = sysinit.DB.Create(&upload).Error
	}
	return
}

// CreateMulti 批量创建文件上传实例
func (s UploadService) CreateMulti(files []*multipart.FileHeader, ctx *gin.Context) (uploads []Upload, errs []error) {
	for _, file := range files {
		if upload, err := s.Create(file, ctx); err == nil {
			errs = append(errs, err)
		} else {
			uploads = append(uploads, upload)
		}
	}
	return
}
